<?php
namespace app\admin\controller;


class Toll extends Base {

    /**
     * 收费记录
     */
    public function index(){
        $this->set_tem_val();

        $where = ['c.is_del'=>0];
        if(input('area_id')){
            $where['c.area_id'] = input('area_id');
        }elseif(input('city_id')){
            $where['c.city_id'] = input('city_id');
        }elseif(input('province_id')){
            $where['c.province_id'] = input('province_id');
        }
        input('name') && $where['c.name'] = trim(input('name'));
        input('account') && $where['c.account'] = trim(input('account'));
        input('principal') && $where['c.principal'] = trim(input('principal'));
        input('mobile') && $where['c.mobile'] = trim(input('mobile'));
        if(input('create_time')){
            $tem = strtotime(input('create_time'));
            $where['cl.create_time'] = [['>=', $tem], ['<=', $tem+86399], 'and'];
        }
        if(input('export') == 1){
            $this->index_explode($where);
        }
        $list = db('charge_log')->alias('cl')
            ->join('company c', 'c.id=cl.com_id', 'LEFT')
            ->where($where)
            ->field('cl.id, cl.create_time, cl.price, c.name, c.account, c.province_id, c.city_id, c.area_id, c.com_mobile, c.principal, c.mobile')
            ->order('cl.id desc')->paginate('',false,page_param());
        $arr_list = $list->items();
        $addr_ids = array_unique(array_merge(array_column($arr_list, 'province_id'), array_column($arr_list, 'city_id'), array_column($arr_list, 'area_id')));
        $region_list = db('region')->where('id', 'in', $addr_ids)->column('id, name');
        $this->assign('list', $list);
        $this->assign('region_list', $region_list);
        return $this->fetch();
    }

    public function index_edit(){
        if(request()->isPost()){
            return $this->index_store();
        }
        $com_list = db('company')->where(['is_del'=>0])->field('id, account')->order('id desc')->select();
        $this->assign('com_list', $com_list);
        return $this->fetch();
    }

    public function index_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $charge_log = db('charge_log')->where('id', 'in', $ids)->field('com_id, price')->select();
        $com_ids = array_unique(array_column($charge_log, 'com_id'));
        $com_list = db('company')->where('id', 'in', $com_ids)->column('id, account');
        $log_data = [];
        foreach($charge_log as $k=>$v){
            if(isset($log_data[$v['com_id']])){
                $log_data[$v['com_id']]['price'] += $v['price'];
            }else{
                $log_data[$v['com_id']]['account'] = $com_list[$v['com_id']];
                $log_data[$v['com_id']]['price'] = $v['price'];
            }
        }
        $log_str = '删除收费记录: ';
        foreach($log_data as $k=>$v){
            $log_str .= "企业账号 {$v['account']} 金额 {$v['price']}元 ";
        }
        add_sys_log($log_str);
        db('charge_log')->where('id', 'in', $ids)->delete();
        return $this->ajaxReturn(0, '成功');
    }

    public function index_store(){
        $params = input();
        if(!$params['com_id'] || !$params['price']){
            return $this->ajaxReturn(1, '添加失败 未选企业账号或价格为空');
        }
        $params['create_time'] = time();
        db('charge_log')->insert($params);
        $com_info = db('company')->where('id', $params['com_id'])->field('account')->find();
        add_sys_log("添加收费记录: 企业账号 {$com_info['account']} 收费金额 {$params['price']}元");
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/index'));
    }

    public function index_explode($where){
        set_time_limit(0);

        $list = db('charge_log')->alias('cl')
            ->join('company c', 'c.id=cl.com_id', 'LEFT')
            ->where($where)
            ->field('cl.id, cl.create_time, cl.price, c.name, c.account, c.province_id, c.city_id, c.area_id, c.com_mobile, c.principal, c.mobile')
            ->order('cl.id desc')->select();
        $addr_ids = array_unique(array_merge(array_column($list, 'province_id'), array_column($list, 'city_id'), array_column($list, 'area_id')));
        $region_list = db('region')->where('id', 'in', $addr_ids)->column('id, name');

        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">企业名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">企业账号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">所在地区</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">公司电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">负责人</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">移动电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">创建时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">收费金额</td>';
        $strTable .= '</tr>';
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['name'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['account'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . "{$region_list[$val['province_id']]}-{$region_list[$val['city_id']]}-{$region_list[$val['area_id']]}" . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['com_mobile'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['principal'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['mobile'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;width:200px;">' . date('Y-m-d H:i:s',$val['create_time']) . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['price'] . '</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .= '</table>';
        unset($list);
        $name = '收费记录';
        downloadExcel($strTable, $name);
        exit();
    }

    /**
     * 财务统计
     */
    public function finance(){
        $where = ['c.is_del'=>0];
        if(input('create_time')){
            $tem = strtotime(input('create_time'));
            $where['cl.create_time'] = ['>=', $tem];
            if(input('create_time2') && input('create_time2') > input('create_time')){
                $tem2 = strtotime(input('create_time2'));
                $where['cl.create_time'] = [['>=', $tem], ['<=', $tem2], 'and'];
            }
        }
        if(input('year_month')){
            $year_month = input('year_month');
            $now_time = time();
            //本年
            if($year_month == 1){
                $year = date('Y');
                $year_start = mktime(0, 0, 0, 1, 1, $year); //本年开始时间戳
                $where['cl.create_time'] = [['>=', $year_start], ['<=', $now_time], 'and'];
            //本季
            }elseif($year_month == 2){
                $year = date('Y');
                $month = date('m');
                $month_start = 1;
                if(1<=$month && $month<=3){
                    $month_start = 1;
                }elseif(4<=$month && $month<=6){
                    $month_start = 4;
                }elseif(7<=$month && $month<=9){
                    $month_start = 7;
                }elseif(10<=$month && $month<=12){
                    $month_start = 10;
                }
                $month_start_time = mktime(0, 0, 0, $month_start, 1, $year); //本季度开始时间戳
                $where['cl.create_time'] = [['>=', $month_start_time], ['<=', $now_time], 'and'];
            //本月
            }else{
                $month_start_time = mktime(0, 0, 0, date('m'), 1, date('Y')); //本月开始时间
                $where['cl.create_time'] = [['>=', $month_start_time], ['<=', $now_time], 'and'];
            }
        }
        if(input('export') == 1){
            $this->finance_explode($where);
        }
        $list = db('charge_log')->alias('cl')
            ->join('company c', 'c.id=cl.com_id', 'LEFT')
            ->where($where)
            ->field('cl.id, cl.create_time, cl.price, c.name, c.account, c.province_id, c.city_id, c.area_id, c.com_mobile, c.principal, c.mobile')
            ->order('cl.id desc')->paginate('',false,page_param());
        $arr_list = $list->items();
        $addr_ids = array_unique(array_merge(array_column($arr_list, 'province_id'), array_column($arr_list, 'city_id'), array_column($arr_list, 'area_id')));
        $region_list = db('region')->where('id', 'in', $addr_ids)->column('id, name');

        $total_price = db('charge_log')->alias('cl')
            ->join('company c', 'c.id=cl.com_id', 'LEFT')
            ->where($where)
            ->field('cl.price')
            ->sum('cl.price');
        $this->assign('list', $list);
        $this->assign('region_list', $region_list);
        $this->assign('total_price', $total_price);
        return $this->fetch();
    }

    public function finance_explode($where){
        set_time_limit(0);

        $list = db('charge_log')->alias('cl')
            ->join('company c', 'c.id=cl.com_id', 'LEFT')
            ->where($where)
            ->field('cl.id, cl.create_time, cl.price, c.name, c.account, c.province_id, c.city_id, c.area_id, c.com_mobile, c.principal, c.mobile')
            ->order('cl.id desc')->select();
        $addr_ids = array_unique(array_merge(array_column($list, 'province_id'), array_column($list, 'city_id'), array_column($list, 'area_id')));
        $region_list = db('region')->where('id', 'in', $addr_ids)->column('id, name');

        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">企业名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:*">企业账号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">所在地区</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">公司电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">负责人</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">移动电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">创建时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">收费金额</td>';
        $strTable .= '</tr>';
        if (!empty($list)) {
            foreach ($list as $key => $val) {
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['name'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['account'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . "{$region_list[$val['province_id']]}-{$region_list[$val['city_id']]}-{$region_list[$val['area_id']]}" . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['com_mobile'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['principal'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['mobile'] . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;width:200px;">' . date('Y-m-d H:i:s',$val['create_time']) . '</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">' . $val['price'] . '</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .= '</table>';
        unset($list);
        $name = '财务统计';
        downloadExcel($strTable, $name);
        exit();
    }
}